package com.dexilaonian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.WorksSizeCheckUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


public class SMS_FindpwdActivity extends BaseActivity {
    @Bind(R.id.title_top)
    TextView titleTop;
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.add)
    ImageView add;
    @Bind(R.id.ed_code)
    EditText edCode;
    @Bind(R.id.time_sms)
    TextView timeSms;
    @Bind(R.id.btn_sms_pwd)
    Button btnSmsPwd;
    @Bind(R.id.send_sms)
    TextView sendSms;
    private String str1;
    private String url;
    private String phone_url;
    private TimeCount time;
    @Override
    protected void initData() {
        //http://agenttest.markmylove.com/Login/LoginApp?
        phone_url = Constant.base_url + "/verify/sentSmsVerify";
        time = new TimeCount(60000, 1000);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_sms__findpwd_activity;
    }

    @Override
    public void initView() {

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        titleTop.setText("找回密码");
        url = Constant.base_url + "/verify/checkSmsVerify";
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        str1 = extras.getString("str1");
        sendSms.setText("已经给号码" + str1 + "发送一条验证码");
        TimeCount time = new TimeCount(60000, 1000);
        time.start();
        WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(btnSmsPwd);
        textChangeListener.addAllEditText(edCode);
        WorksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    btnSmsPwd.setTextColor(getResources().getColor(R.color.white));
                    btnSmsPwd.setBackground(getResources().getDrawable(R.drawable.btn_login2));
                } else {
                    btnSmsPwd.setTextColor(getResources().getColor(R.color.gray));
                    btnSmsPwd.setBackground(getResources().getDrawable(R.drawable.btn_login1));
                }
            }
        });
    }

    @OnClick({R.id.back, R.id.time_sms, R.id.btn_sms_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.time_sms:
                sendPhone(str1);
                break;
            case R.id.btn_sms_pwd:
                String code = edCode.getText().toString().trim();
                ensurever(code);
                break;
        }
    }
    private void sendPhone(String phone) {
        showProgressBar(true);
        OkHttpUtils.post().url(phone_url).addParams("phone",phone).addParams("type","1").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
                showProgressBar(false);
            }
            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                Log.e("TTTforget2222", response);
                try {
                    JSONObject object = new JSONObject(response);
                    int status = Integer.parseInt(object.optString("status"));
                    if (status == 6) {
                        showToast("单手机号单日发送短信超10条");
                        return;
                    }
                    if (status == 7) {
                        showToast("单手机号短信发送时间间隔少于1分钟");
                    }
                    if (status==0) {
                       time.start();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
    private void ensurever(String code) {
        timeSms.setClickable(false);
        OkHttpUtils.post().url(url).addParams("phone", str1).addParams("code", code).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }
            @Override
            public void onResponse(String response, int id) {
                Log.e("TTTsms", response);
                try {
                    JSONObject object = new JSONObject(response);
                    int status = Integer.parseInt(object.optString("status"));

                    if (status == 0) {
                        Intent intent = new Intent(SMS_FindpwdActivity.this, Input_NewPwdActivity.class);
                        intent.putExtra("str2", str1);
                        startActivity(intent);
                    } else {
                        showToast("验证码不正确");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * 计时器
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {
            //计时完毕时触发
            timeSms.setText("重发");
            timeSms.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {//计时过程显示
            if (timeSms!=null) {
                timeSms.setText(millisUntilFinished / 1000 + "秒");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
      //  time.cancel();
    }
}
